package com.atguigu.gulimall.ware.service.impl;

import com.alibaba.fastjson.TypeReference;
import com.atguigu.common.utils.R;
import com.atguigu.gulimall.ware.feign.MemberFeignService;
import com.atguigu.gulimall.ware.wareVo.FareVo;
import com.atguigu.gulimall.ware.wareVo.MemberAddressVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.ware.dao.WareInfoDao;
import com.atguigu.gulimall.ware.entity.WareInfoEntity;
import com.atguigu.gulimall.ware.service.WareInfoService;
import org.springframework.util.StringUtils;


@Service("wareInfoService")
public class WareInfoServiceImpl extends ServiceImpl<WareInfoDao, WareInfoEntity> implements WareInfoService {

    @Autowired
    MemberFeignService memberService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        LambdaQueryWrapper<WareInfoEntity> wrapper = new LambdaQueryWrapper<WareInfoEntity>();
        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)){
            wrapper.and((w)->{
                w.eq(WareInfoEntity::getId,key).or()
                        .like(WareInfoEntity::getName,key).or()
                        .like(WareInfoEntity::getAddress,key).or()
                        .like(WareInfoEntity::getAreacode,key);
            });

        }
        IPage<WareInfoEntity> page = this.page(
                new Query<WareInfoEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }
    //根据地址查询运费
    @Override
    public FareVo getFace(Long addrId) {
        FareVo fareVo = new FareVo();
        R info = memberService.info(addrId);
        MemberAddressVo memberAddressVo = info.getData("memberReceiveAddress",new TypeReference<MemberAddressVo>() {
        });
        if (memberAddressVo != null){
            String phone = memberAddressVo.getPhone();
            String substring = phone.substring(phone.length() - 1, phone.length());
            BigDecimal bigDecimal = new BigDecimal(substring);
            fareVo.setMemberAddressVo(memberAddressVo);
            fareVo.setFare(bigDecimal);
            return fareVo;
        }
        return null;
    }

}